package com.xgkj.diyiketang.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xgkj.diyiketang.data.DesireInfo;
import com.xgkj.diyiketang.http.CouponHttpRequest;
import com.xgkj.diyiketang.http.CouponHttpResponse;
import com.xgkj.diyiketang.http.engine.CouponHttpBaseTask;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by windo on 17/1/3.
 */

public class YuanwangListTask extends CouponHttpBaseTask {
    private int type;
    private int pageNo;
    private int pageSize;

    public YuanwangListTask(int ywtype, int currentPage, int size) {
        super();
        type = ywtype;
        pageNo = currentPage;
        pageSize = size;
    }

    @Override
    protected void processResponseData(CouponHttpResponse response) {
        JSONObject res_json = response.getBody();
        int pageCount = 0;
        int currentPage = 0 ;
        int nextPage = 0;
        List<DesireInfo> list = new ArrayList<>();
        if (res_json != null){
            JSONObject json = res_json.getJSONObject("data");
            pageCount = json.getIntValue("pageCount");
            currentPage = json.getIntValue("currentPage");
            nextPage = currentPage>=pageCount ? 0 : (currentPage+1);
            String listString = json.getString("list");
            list = JSON.parseArray(listString,DesireInfo.class);
        }
        notifyTaskCompleted(nextPage,list);
    }

    @Override
    protected void prepareRequest() {
        super.prepareRequest();
        if (mPhase == TASK_PHASE_INIT){
            CouponHttpRequest request = new CouponHttpRequest();
            request.setApiNo(ApiNo.YUANWANG_LIST);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("pageNo",pageNo);
            jsonObject.put("size",pageSize);
            jsonObject.put("type",type);
            request.setData(jsonObject.toString());
            addRequest(request);
        }
    }
}
